<template>
  <div>
    <div :class="bool === true ? 'header active' : 'header'" id="header">
      <div class="navigation">
        <div class="nav_left">
          <img src="@/assets/logo.png" alt="" />
          <span>人工智能应用技术创新中心</span>
        </div>
        <div class="nav_center">
          <ul>
            <li v-for="item of navList" :key="item.index">
              <a href="javascript:void(0);" @click="skip(item.listUrl)">{{
                item.listName
              }}</a>
              <ul class="dropdown" v-if="item.listName === '企业简介'">
                <li
                  class="dropdownContent"
                  v-for="i of dropdownList1"
                  :key="i.index"
                >
                  <a href="javascript:void(0);" @click="skip(i.cUrl)">{{
                    i.cName
                  }}</a>
                </li>
              </ul>
              <ul class="dropdown" v-if="item.listName === '新闻中心'">
                <li
                  class="dropdownContent"
                  v-for="i of dropdownList2"
                  :key="i.index"
                >
                  <a href="javascript:void(0);" @click="skip(i.ccUrl)">{{
                    i.ccName
                  }}</a>
                </li>
              </ul>
            </li>
          </ul>
        </div>
        <div class="nav_right">
          <span>177 5492 8306</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navList: [
        { listName: "首页", listUrl: "/Home" },
        { listName: "企业简介", listUrl: "/CompanyIntro" },
        { listName: "新闻中心", listUrl: "/CompanyNews" },
        { listName: "作品案例", listUrl: "/Production" },
        { listName: "服务项目", listUrl: "/Service" },
        { listName: "知识在线", listUrl: "/Intelligence" },
      ],
      dropdownList1: [
        { cName: "公司简介", cUrl: "/CompanyIntro" },
        { cName: "企业文化", cUrl: "/CompanyCulture" },
      ],
      dropdownList2: [
        { ccName: "公司新闻", ccUrl: "/CompanyNews" },
        { ccName: "行业新闻", ccUrl: "/TradeNews" },
      ],
      bool: false,
    };
  },

  created() {
    // 给window添加一个滚动滚动监听事件
    window.addEventListener("scroll", this.handleScroll);
  },
  mounted() {
    // this.t = window.sessionStorage.getItem("lastname");
  },
  // 注意，如果离开该页面需要移除这个监听的事件，不然会报错
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    handleScroll() {
      /* 
      整体是获取页面滚动高度
      1.window.pageYOffset即scrollY 它会返回文档当前沿垂直轴（即向上或向下）滚动的像素数，其值为0.0，
      表示该Document的顶部边缘当前与窗口内容区域的顶部边缘对齐
      2.document.documentElement.scrollTop 获取当前页面的滚动条纵坐标位置 对应的是 html 标签
      3.document.body.scrollTop  对应的是 body 标签
      (||这个是定义时必须加的或者而不是定义时的另一种写法)
      */
      var scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      // 监听元素到顶部的距离  并判断滚动的距离如果大于了元素到顶部的距离时，设置bool为true,否则就是false
      var offsetTop = document.querySelector("#header").offsetTop;
      if (scrollTop > offsetTop) {
        this.bool = true;
      } else {
        this.bool = false;
      }
      // console.log(this.bool)
    },
    // 跳转
    skip(url) {
      // if (name === "企业文化" || name === "行业新闻") {
      // this.t = "2";
      // window.sessionStorage.setItem("lastnumber", this.t);
      // this.$router.push({ path: url });
      // console.log(this.t);
      // } else {
      // this.t = "1";
      // window.sessionStorage.setItem("lastnumber", this.t);
      // console.log(this.t);
      // console.log(url);
      this.$router.push({ path: url });
      // }
    },
  },
};
</script>

<style lang="scss">
// 顶部下拉菜单
@mixin dropdown {
  position: absolute;
  display: flex;
  flex: 1;
  flex-direction: column;
}
@mixin dropdownContent {
  display: none;
  // position: absolute;
  border: 1px solid pink;
  background-color: #f9f9f9;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  padding: 0 20px;
  box-sizing: border-box;
  line-height: 40px;
  font-size: 14px;
}

// 导航栏下滑时
.active {
  background: rgb(16, 19, 35, 0.9) !important;
}
// 导航栏部分
.header {
  width: 100%;
  height: 72px;
  min-width: 1300px;
  box-sizing: border-box;
  position: fixed;
  z-index: 999;
  background: rgb(0, 0, 0, 0);
  .navigation {
    width: 1200px;
    height: 72px;
    margin: 0 auto;
    line-height: 72px;
    display: flex;
    justify-content: space-between;
    .nav_left {
      width: 400px;
      display: flex;
      align-items: center;
      img {
        height: 50px;
        width: 50px;
      }
      span {
        font-size: 20px;
        color: #fff;
        margin-left: 17px;
      }
    }
    .nav_center {
      ul {
        height: 72px;
        list-style: none;
        display: flex;
        flex-direction: row;
        li {
          display: block;
          transition: 0.2s ease-in-out;
          a {
            text-decoration: none;
            color: #fff;
            padding: 0 20px;
          }
          .dropdown {
            @include dropdown;
          }
          .dropdownContent {
            @include dropdownContent;
            a {
              padding: 0;
            }
          }
        }
        li:hover {
          background-color: #1c2139;
        }
        li:hover .dropdownContent {
          display: block;
          background: pink;
          width: 100px;
        }
      }
    }
    .nav_right {
      height: 72px;
      line-height: 72px;
      span {
        font-size: 18px;
        color: #fff;
        height: 72px;
        padding-left: 32px;
        background: url(../../assets/header-tel.png) no-repeat left center;
      }
    }
  }
  transition: 0.5s ease-in-out;
  &:hover {
    background: #1c2139;
  }
}
</style>